const template = document.createElement("template");

template.innerHTML = /*html*/ `
<style>
:host {
  display: block;
  font-size: 13px;
  transition: all 0.5s linear;
  width: 100%;
  height: 100%;
}
:host *{
  box-sizing: border-box;
}

/* 滚动槽 */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  border-radius: 3px;
  background: transparent;
  -webkit-box-shadow: inset 0 0 5px transparent;
}
/* 滚动条滑块 */
::-webkit-scrollbar-thumb {
  border-radius: 3px;
  background: rgba(0,0,0,0.12);
  -webkit-box-shadow: inset 0 0 10px rgba(0,0,0,0.2);
}

.null{
    width: 100%;
    height: calc(100% - 30px);
    padding: 10px;
    color: rgba(160, 160, 160, 0.6);
}

ul,li{
  padding: 0;
  margin: 0;
  list-style: none;
}


.notes-box{
  width: 100%;
  height: 100%;
  overflow: hidden;
}
ul{
  width: 100%;
  height: calc(100% - 30px);
  overflow-x: hidden;
  overflow-y: auto;
  position: relative;
}
li{
  width: 100%;
  position: relative;
  padding: 10px 10px 10px 0;
  padding-left: 40px;
}
li .title{
  font-size: 12px;
  color: rgba(0, 0, 0, 0.5);
}
li:hover{
  background-color: rgba(0, 0, 0, 0.05);
}

li .icons{
  position: absolute;
  right: -50px;
  top: 0;
  display: flex;
  height: 100%;
  align-items: center;
  transition: all 0.4s;
  font-size: 14px;
  cursor: pointer;
}

li:hover .icons{
  right: 10px;
}

ul::before {
  position: absolute;
  content: '';
  display: block;
  left: 19.5px;
  top: 10px;
  width: 1px;
  height: calc(100% - 10px);
  background-color: var(--theme-week-bg);
}
li::before{
  position: absolute;
  content: '';
  display: block;
  left: calc((40px - 1.2em) / 2);
  top: 10px;
  width: 1.2em;
  height: 1.2em;
  border-radius: 50%;
  border: 1px solid var(--theme-week-bg);
  background-color: var(--theme-bg);
  transform: rotate(10deg);
}
li.checked::before {
  content: '✔';
  text-align: center;
  font-size: 12px;
  line-height: 1.2em;
  color: var(--theme-week-bg);
}

li .content{
  word-break: break-all;
  margin-top: 5px;
  line-height: 1.7em;
}
.notes-footer{
  width: 100%;
  height: 30px;
  padding: 5px 10px;
  user-select: none;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
  font-size: 12px;
}
</style>
<div class="notes-box">
  <div class="null">
    新增一条：双击空白处，或双击底部栏目位置
  </div>
  <ul>
    <li>
      <div class="title">2024-08-13 13:14</div>
      <div>新增一条：双击空白处，或鼠标右键，或点顶部+号，或双击底部栏目位置</div>
    </li>
    <li>
      <div class="title">2024-08-13 13:14</div>
      <div>新增一条：双击空白处，或鼠标右键，或点顶部+号，或双击底部栏目位置</div>
    </li>
    <li>
      <div class="title">2024-08-13 13:14</div>
      <div>新增一条：双击空白处，或鼠标右键，或点顶部+号，或双击底部栏目位置</div>
    </li>
  </ul>
  <div class="notes-footer">时间轴</div>
</div> 
`;


export default class Notes extends HTMLElement {
  static observedAttributes = ["value"];
  constructor() {
    super();
    this.value = [
      {
        content: '1新增一条：双击空白处，或鼠标右键，或点顶部+号，或双击底部栏目位置',
        time: 1723600117303,
        remind: false
      }
    ];
    this.editIndex = null;
    const shadow = this.attachShadow({ mode: "open" });
    const content = template.content.cloneNode(true); 

    this.ul_el = content.querySelector('ul');
    this.null_el = content.querySelector('.null');
    content.querySelector('.notes-footer').ondblclick = (e) => {
      window.openPopup('./haoyongnotesPendant/popup/time-notes.html', {
        width: 500,
        height: 320,
        x: e.screenX,
        y: e.screenY,
      })
    }

    this.null_el.ondblclick = (e) => {
      window.openPopup('./haoyongnotesPendant/popup/time-notes.html', {
        width: 500,
        height: 320,
        x: e.screenX,
        y: e.screenY,
      })
    }

    if(window.ipcRenderer){
      window.ipcRenderer.on('time-notes-data', (event, data)=>{
        if(this.editIndex === null){
          this.value.push(data);
        }else{
          this.value[this.editIndex] = data;
        }
        this.dispatchEventHandler();
        this.renderList();
        this.editIndex = null;
      });
    }

    this.renderList();
 
    shadow.appendChild(content);
  }

  renderList(){
    if(this.value.length === 0){
      this.null_el.style.display = 'block';
      this.ul_el.style.display = 'none';
      return;
    }else{
      this.null_el.style.display = 'none';
      this.ul_el.style.display = 'block';
    }

    // 排序 
    this.value.sort((a, b) => {
      return b.time - a.time;
    })
    this.ul_el.innerHTML = this.value.map((item,index) => {
      let date = new Date();
      let time = date.getTime();
      let checked = item.time < time;
      let currentDate = new Date(item.time);
      let year = currentDate.getFullYear();
      let month = (currentDate.getMonth() + 1) < 10 ? '0' + (currentDate.getMonth() + 1) : currentDate.getMonth() + 1;
      let day = currentDate.getDate() < 10 ? '0' + currentDate.getDate() : currentDate.getDate();
      let hour = currentDate.getHours() < 10 ? '0' + currentDate.getHours() : currentDate.getHours();
      let minute = currentDate.getMinutes() < 10 ? '0' + currentDate.getMinutes() : currentDate.getMinutes();

      return `
        <li class="${checked ? 'checked' : ''}">
          <div class="title">${year}-${month}-${day} ${hour}:${minute}</div>
          <div class="content">${item.content}</div>
          <div class="icons"><icon-svg name="closure"></div>
        </li>
      `
    }).join('');

    this.ul_el.querySelectorAll('li').forEach((item,index)=>{
      item.ondblclick = (e) => {
        e.stopPropagation();
        this.editIndex = index;
        window.openPopup('./haoyongnotesPendant/popup/time-notes.html', {
          width: 500,
          height: 320,
          x: e.screenX,
          y: e.screenY,
          data: JSON.stringify(this.value[index]),
        })
      }
    })

    this.ul_el.querySelectorAll('icon-svg[name=closure]').forEach((item,index)=>{
      item.onclick = ()=>{
        this.value.splice(index,1);
        this.renderList();
        this.dispatchEvent(new CustomEvent("change", {
          detail: {
            value: this.value
         }
        }))
      }
    })
  }

  dispatchEventHandler () {
    this.dispatchEvent(new CustomEvent("change", {
      detail: {
        value: this.value,
      },
    }));
  }

  connectedCallback () {
  }

  attributeChangedCallback(name, oldValue, newValue) {
    console.log(name);
  

    if(name === 'value'){
      this.value = JSON.parse(newValue);
      this.renderList();
    }
  }
  
}